package org.sanmao.wyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.sanmao.wyj.controller.utils.R;
import org.sanmao.wyj.controller.utils.Result;
import org.sanmao.wyj.dao.OrderDao;
import org.sanmao.wyj.domain.Conment;
import org.sanmao.wyj.domain.Order;
import org.sanmao.wyj.domain.User;
import org.sanmao.wyj.domain.Worker;
import org.sanmao.wyj.exception.CreatorIsNotMeException;
import org.sanmao.wyj.exception.MoneyIsNotEnoughException;
import org.sanmao.wyj.service.ConmentService;
import org.sanmao.wyj.service.OrderService;
import org.sanmao.wyj.service.UserService;
import org.sanmao.wyj.service.WorkerService;
import org.sanmao.wyj.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao,Order> implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private UserService userService;
    @Autowired
    private WorkerService workerService;
    @Autowired
    private ConmentService conmentService;

    @Override
    public List<Order> findAllOrders() {
        List<Order> orders = orderDao.findAllOrders();
        return orders;
    }

    @Override
    public List<Order> findOrderByUid(int id) {
        List<Order> orders = orderDao.findOrderByUid(id);
        return orders;
    }

    @Override
    public IPage<Order> findOrderByUidAndType(int id, int type, int currentPage, int pageSize) {
        IPage<Order> page = new Page<>(currentPage,pageSize);
        IPage orders = orderDao.findOrderByUidAndType(page,id,type);
        return orders;
    }

    @Override
    public Order findOrderByOid(int id) {
        Order order = orderDao.selectById(id);
        return order;
    }

    @Override
    public Boolean deleteOrderByOid(int id) {
        Order order = orderDao.selectById(id);
        int cid = order.getCid();
        if (orderDao.deleteById(id) == 1){
            conmentService.removeById(cid);
            return true;
        }
        else return false;
    }

    @Override
    public IPage<Order> fuzzyFindOrders(String con, int currentPage, int pageSize) {
        IPage<Order> page = new Page<>(currentPage,pageSize);
        return orderDao.fuzzyFindOrders(page,con);
    }

    @Override
    public IPage<Order> findOrdersPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        orderDao.selectPage(page,null);
        return page;
    }

    @Override
    public int addOrder(Order order, HttpServletRequest request) {
        String token = request.getHeader("token");
        int userId = JwtUtils.getUserId(token);
        order.setUid(userId);
        Date date = new Date(System.currentTimeMillis());
        order.setTime(date);
        order.setState(1);
        User user = userService.findUserById(userId);
        double balance = user.getBalance();
        try{
            if(order.getMoney()>balance){
                throw new MoneyIsNotEnoughException("余额不足");
            }
            else{
                user.setBalance(balance-order.getMoney());
                userService.updateUser(user);
                return orderDao.insert(order);
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            return -1;
        }

    }

    @Override
    public IPage<Order> fuzzyFindOrderByConAndUidAndType(String con, int type,int currentPage, int pageSize, HttpServletRequest request) {
        IPage<Order> page = new Page<>(currentPage,pageSize);
        String token = request.getHeader("token");
        int userId = JwtUtils.getUserId(token);
        IPage<Order> orderPage = orderDao.fuzzyFindOrderByConAndUidAndType(page,userId, con, type);
        return orderPage;
    }

    @Override
    public IPage<Order> findOrderByState(int currentPage, int pageSize, int state,HttpServletRequest request) {
        String token = request.getHeader("token");
        int id = JwtUtils.getUserId(token);
        IPage<Order> page = new Page<>(currentPage,pageSize);
        return orderDao.findOrderByState(page,state,id);
    }

    @Override
    public IPage<Order> findOrderByType(int type, int currentPage, int pageSize) {
        IPage<Order> page = new Page<>(currentPage,pageSize);
        return orderDao.findOrderByType(page,type);
    }

    @Override
    public IPage<Order> fuzzyFindOrderByConAndType(String con, int type, int currentPage, int pageSize) {
        IPage<Order> page = new Page<>(currentPage,pageSize);
        return orderDao.fuzzyFindOrderByConAndType(page,con,type);
    }

    @Override
    public int takeOrder(HttpServletRequest request, int oid) {
        String token = request.getHeader("token");
        int id = JwtUtils.getUserId(token);
        Order order = orderDao.selectById(oid);
        order.setWid(id);
        order.setState(2);
        return orderDao.updateById(order);
    }

    @Override
    public Order findFullOrderByOid(int oid) {
        return orderDao.findFullOrderByOid(oid);
    }


    @Override
    public IPage<Order> workerFindOrderByState(int state, int currentPage, int pageSize,HttpServletRequest request) {
        String token = request.getHeader("token");
        int id = JwtUtils.getUserId(token);
        IPage<Order> page = new Page<>(currentPage,pageSize);
        return orderDao.workerFindOrderByState(page,state,id);
    }

    @Override
    public int overOrder(int oid,HttpServletRequest request) {
        String token = request.getHeader("token");
        int wid = JwtUtils.getUserId(token);
        Worker worker = workerService.findWorkerById(wid);
        Order order = orderDao.selectById(oid);
        worker.setBalance(worker.getBalance()+order.getMoney());
        if(workerService.updateWorker(worker)){
            order.setState(3);
            return orderDao.updateById(order);
        }
        else return 0;
    }

    @Override
    public String findUserPhonenumByOid(int oid) {
        return orderDao.findUserPhonenumByOid(oid);
    }

    @Override
    public String findWorkerPhonenumByWid(int oid) {
        return orderDao.findWorkerPhonenumByOid(oid);
    }

    @Override
    public IPage<Order> fuzzyFindOrderByConAndUidAndState(String con, int state, int currentPage, int pageSize, HttpServletRequest request) {
        String token = request.getHeader("token");
        int id = JwtUtils.getUserId(token);
        IPage<Order> page = new Page<>(currentPage,pageSize);
        return orderDao.fuzzyFindOrderByConAndState(page,con,state,id);
    }


}
